﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using ELS.User;

namespace ELS.BackupRestore
{
    public partial class BackUpForm : MdiChildForm
    {
        Panel parentPanel;
        TextBox textBox;
        PictureBox pictureBox;

        public BackUpForm()
        {
            InitializeComponent();
        }
        BackupRestoreController bkController = new BackupRestoreController();
        public BackUpForm(Panel pnlMainForm)
        {
            InitializeComponent();
            this.parentPanel = pnlMainForm;
            LoadListFileData();
        }
        public BackUpForm(TextBox textBox, PictureBox pictureBox, Panel pnlMainForm)
        {
            InitializeComponent();
            this.textBox = textBox;
            this.pictureBox = pictureBox;
            this.parentPanel = pnlMainForm;
            LoadListFileData();
        }
        private void LoadListFileData()
        {
            drgBackUpList.AutoGenerateColumns = false;
            drgBackUpList.DataSource = bkController.GetListFileData();
        }
        private void btnAddBkFile_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogConfirm = MessageBox.Show(UserCommon.Add_BackupFile_Message, UserCommon.Add_BackupFile_MessageBox_Title, MessageBoxButtons.YesNo);
                if (dialogConfirm == DialogResult.Yes)
                {             
                    bool isAddNewFileSuccess = bkController.AddNewFile() > 0;
                    if (isAddNewFileSuccess)
                    {
                        LoadListFileData();
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.AddBackUpFile_Success, UserCommon.DONE_STATUS);
                    }
                    else
                    {
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.AddBackUpFile_Fail, UserCommon.FAIL_STATUS);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }  
        }
        private List<string> GetListRowSelect()
        {
            List<string> rowsSelect = new List<string>();
            rowsSelect.Clear();
            foreach (DataGridViewRow row in drgBackUpList.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    rowsSelect.Add(row.Cells["ID"].Value.ToString());
                }
            }
            return rowsSelect;
        }
        private void btnDeleteBkFile_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> listFileId = GetListRowSelect();
                if(listFileId.Count > 0)
                {
                    DialogResult dialogConfirm = MessageBox.Show(UserCommon.Delete_BackupFile_Message, UserCommon.Delete_BackupFile_MessageBox_Title, MessageBoxButtons.YesNo);
                    if (dialogConfirm == DialogResult.Yes)
                    {


                        bool isDeleteSuccess = bkController.DeleteBackUpFile(listFileId);
                        if (isDeleteSuccess)
                        {
                            UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteBackUpFile_Success, UserCommon.DONE_STATUS);
                            // Reload content
                            LoadListFileData();
                        }
                        else
                        {
                            UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteBackUpFile_Fail, UserCommon.FAIL_STATUS);
                        }
                    }
                    
                }
                else
                {
                    UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteBackUpFile_DoNotSelect, UserCommon.WARNING_STATUS);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            } 
        }
    }
}
